package com.xyw.code.spingbootoath2demojdbc1.security;

import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义客户端查找加载器
 * 增加了一个缓存的功能
 */
public class CustomClientDetailsService extends JdbcClientDetailsService {
    private static final Map<String, ClientDetails> CLIENTS = new HashMap<>();

    public static void clearCache() {
        CLIENTS.clear();
    }
    public static void clearCache(String clientId) {
        CLIENTS.remove(clientId);
    }

    public CustomClientDetailsService(DataSource dataSource) {
        super(dataSource);
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {
        //先判断缓存是否有  有的话 直接取缓存
        ClientDetails clientDetails = CLIENTS.get(clientId);
        if(clientDetails!=null){
            return clientDetails;
        }
        clientDetails = super.loadClientByClientId(clientId);
        CLIENTS.put(clientId,clientDetails);
        return clientDetails;
    }
}
